************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates main paper, Figure 2 (Baseline Distributions of Sleep-Related Variables (RCT Sample))
* Last edited: 07 May 2021
************************************************


	************************************************	
	*0. Initial Setup
	************************************************

	clear all
	set more off
	set matsize 800
	

************************************************	
*1. First stage: 
************************************************

	*Prep data
	
		*Analysis base file
		use "$d/firststage_dataset.dta", clear
		
		gen treat_int = treat_pool*treat_nap
		gen treat_int_s = treat_s*treat_nap
		gen treat_int_s_i = treat_s_i*treat_nap
		
		gen treat_pool_cell = treat_pool*(1-treat_nap)
		gen treat_s_cell = treat_s*(1-treat_nap)
		gen treat_s_i_cell = treat_s_i*(1-treat_nap)
		gen treat_nap_cell = treat_nap*(1-treat_pool)
		gen treat_int_cell = treat_int 
		gen treat_int_s_cell = treat_int_s
		gen treat_int_s_i_cell = treat_int_s_i
		
		replace nap_time_mins = .1 if pid == 5130 & day_in_study == 27 //raw actigraph data incorrectly recorded as half hour nap
	
		* Mean Daily Graphs
		replace  nap_time_mins = 0 if post_treatment == 0

	* Mean Daily Graphs - Fully Disaggregated
		gen treatment_disag = 1 if treat_s_cell == 1
		replace treatment_disag = 2 if treat_s_i_cell == 1
		replace treatment_disag = 3 if treat_nap_cell == 1
		replace treatment_disag = 4 if treat_int_s_cell == 1
		replace treatment_disag = 5 if treat_int_s_i_cell == 1
		replace treatment_disag = 0 if mi(treatment_disag)

	
		rename epoch_sleepefficiency epoch_sleepeff
		rename self_report self_report_sleep
		rename sleep_report self_report_inbed
		
		local t0="Actigraph Sleep (Hours/Night)"
		local t1="Actigraph Sleep Efficiency (Sleep/Time in Bed)"
		local t2="Self-reported Sleep (Hours/Night)"
		local t3="Actigraph Time in Bed (Hours/Night)"
		local t4="Self-reported Time in Bed (Hours/Night)"
		local t5="Self-reported Efficiency (Sleep/Time in Bed)"
		local l0 = 7
		local l1 = 0
		local x=0
		
		foreach var in Sleep_Night epoch_sleepeff self_report_sleep act_inbed self_report_inbed self_report_eff {
		preserve
		keep if post_treatment == 0
			bysort pid: egen ind_mean_`var' = mean(`var')
			bysort pid: gen single_entry_`var' = _n
			sum `var', d
			local m : di %3.2f `r(mean)'
						
			if "`var'" == "Sleep_Night" {
			#d;
				twoway function y= .2 ,range(7 9) recast(area) color(gs12) || 
				(hist ind_mean_`var' if single_entry_`var' == 1 & ind_mean_`var' > 3, fraction 
				xtitle("`t`x''", size(large))
				text(0.16 `m' "Mean: `m' hours", place(w) size(large))
				text(0.16 8 "Recommended" "Sleep Duration" "by Experts", place(c) size(medium))
				ytitle(Percent, size(large)) 
				graphregion(fcolor(white)) 
				xscale(r(3 9)) xtick(3(1)9) xlabel(3(1)9)
				fcolor(navy) lcolor(white) 
				legend(off)) 
				(scatteri 0 `m' 0.2 `m', recast(line) color(maroon) lwidth(thick))
			;
			#d cr
			}
			
			if "`var'" == "self_report_sleep" {
			
			
				#d;
				twoway function y= .2 ,range(7 9) recast(area) color(gs12) || 
				(hist
				ind_mean_`var' if single_entry_`var' == 1 & ind_mean_`var'<9, fraction 
				xtitle("`t`x''", size(large))
				text(0.16 `m' "Mean: `m' hours", place(w) size(large))
				text(0.16 8 "Recommended" "Sleep Duration" "by Experts", place(c) size(medium))
				ytitle(Percent, size(large)) 
				graphregion(fcolor(white)) 
				xscale(r(3 9)) xtick(3(1)9) xlabel(3(1)9)
				fcolor(navy) lcolor(white) 
				legend(off))
				(scatteri 0 `m' 0.2 `m', recast(line) color(maroon) lwidth(thick))
			;
			#d cr
			}
			
			if "`var'" == "act_inbed" {
			#d;
				twoway (hist
				ind_mean_`var' if single_entry_`var' == 1, fraction 
				xtitle("`t`x''", size(large))
				text(0.16 `m' "Mean: `m' hours", place(w) size(large))
				ytitle(Percent, size(large)) 
				graphregion(fcolor(white))
				yscale(r(0 .16)) ytick(0(0.05).16) ylabel(0(0.05).16)
				xscale(r(3 12)) xtick(3(1)12) xlabel(3(1)12)
				fcolor(navy) lcolor(white) 
				legend(off))
				(scatteri 0 `m' 0.2 `m', recast(line) color(maroon) lwidth(thick)) 
			;
			#d cr
			}
			
			if "`var'" == "self_report_inbed" {
			#d;
				twoway (hist
				ind_mean_`var' if single_entry_`var' == 1, fraction 
				xtitle("`t`x''", size(large))
				text(0.16 `m' "Mean: `m' hours", place(w) size(large))
				ytitle(Percent, size(large)) 
				graphregion(fcolor(white))
				yscale(r(0 .2)) ytick(0(0.05).2) ylabel(0(0.05).2)
				xscale(r(3 12)) xtick(3(1)12) xlabel(3(1)12)
				fcolor(navy) lcolor(white) 
				legend(off))
				(scatteri 0 `m' 0.2 `m', recast(line) color(maroon) lwidth(thick))
			;
			#d cr
			}
			
			if "`var'" == "epoch_sleepeff" {
				local mpct = round(100 * `r(mean)', 1)
				#d;
				twoway function y= .2 ,range(.85 1) recast(area) color(gs12) || 
				(hist
				ind_mean_`var' if single_entry_`var' == 1 & ind_mean_`var'<1,  fraction 
				xtitle("`t`x''")
				text(0.16 `m' "Mean: `mpct'%", place(w) size(large))
				text(0.18 0.93 "Normal" "Sleep" "efficiency", place(c) size(large))
				ytitle(Percent, size(large))
				xscale(r(0.2 1)) xtick(0.2(0.2)1) xlabel(0.2(0.2)1)
				graphregion(fcolor(white)) 
				fcolor(navy) lcolor(white) 
				legend(off))
				(scatteri 0 `m' 0.2 `m', recast(line) color(maroon) lwidth(thick))
			;
			#d cr
			}
			
			if "`var'" == "self_report_eff" {
			local mpct = round(100 * `r(mean)', 1)
			#d;
				twoway function y= 0.6 ,range(.85 1) recast(area) color(gs12) || 
				(hist
				ind_mean_`var' if single_entry_`var' == 1, fraction 
				xtitle("`t`x''", size(large))
				text(0.48 `m' "Mean: `mpct'%", place(w) size(large))
				text(0.55 0.77 "Normal" "Sleep" "Efficiency", place(c) size(large))
				xscale(r(0.2 1)) xtick(0.2(0.2)1) xlabel(0.2(0.2)1)
				yscale(r(0 0.6))
				ytitle(Percent) 
				graphregion(fcolor(white)) 
				fcolor(navy) lcolor(white) 
				legend(off) 
				bin(6))
				(scatteri 0 `m' 0.6 `m', recast(line) color(maroon) lwidth(thick))
			;
			#d cr
			}
			
						
			*Save graphs

				graph export "$om/Figures/Figure2/Hist_`var'_baseline.eps", replace	
				graph export "$om/Figures/Figure2/Hist_`var'_baseline.png", replace	
			
		local x = `x' + 1		
		restore
		}
